package thread;

public class ThreadDemo9 {
    public static boolean flag = true;

    public static void main(String[] args) throws InterruptedException{
        Thread thread = new Thread(() -> {
            while (flag) {
                System.out.println("hello");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

        Thread.sleep(3000);//3秒后结束线程
        //主线程这里可以随时通过flag变量的取值，来操作 thread 线程是否结束。
        flag = false;
    }
}
